package com.heatup.apt.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.common.constants.DataDictConstants;
import com.heatup.apt.common.util.JsonUtils;
import com.heatup.apt.management.service.DataDictService;
import com.heatup.apt.management.service.ResourceService;
import com.heatup.apt.management.service.coach.CoachService;
import com.heatup.apt.management.service.course.CourseService;
import com.heatup.apt.management.service.course.CourseStandardService;
import com.heatup.apt.model.course.CourseStandard;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
/**
 * Created by b3st9u on 16/9/21.
 * updated by blank on 16/10/12.
 */
@Controller
@RequestMapping("system/course")
public class CourseStandardController extends BaseController {
    @Autowired
    CourseStandardService courseStandardService;
    @Autowired
    CoachService coachService;
    @Autowired
    ResourceService resouceService;
    @Autowired
    DataDictService dataDictService;
    @Autowired 
    CourseService courseService ;

    /**
     * 标准课程新增页面跳转
     * @return
     */
    @RequestMapping(value = "/to_course_standard_add")
    public ModelAndView toCourseStandardAdd() {
        ModelAndView modelAndView = new ModelAndView("course/course-standard-add");
        modelAndView.addObject("coachList", coachService.queryAllCoachInfo());
        modelAndView.addObject("dataDictList", JsonUtils.obj2Json(dataDictService.queryDataDicts(DataDictConstants.DICT_TYPE_COURSE_ATTR)));
        return modelAndView;
    }

    /**
     * 标准课程编辑页面跳转
     * @return
     */
    @RequestMapping(value = "/to_course_standard_edit")
    public ModelAndView toCourseStandardedit(@RequestParam int id) {
        ModelAndView modelAndView = new ModelAndView("course/course-standard-edit");
        CourseStandard courseStandard = courseStandardService.queryCourseStandardById(id);
        modelAndView.addObject("courseStandard", courseStandard);
        modelAndView.addObject("courseStandardJson", JsonUtils.obj2Json(courseStandard));

        modelAndView.addObject("coachList", coachService.queryAllCoachInfo());
        modelAndView.addObject("dataDictList", JsonUtils.obj2Json(dataDictService.queryDataDicts(DataDictConstants.DICT_TYPE_COURSE_ATTR)));
        return modelAndView;
    }

    /**
     * 标准课程保存
     * @return
     */
    @RequestMapping(value = "/course_standard_save")
    @ResponseBody
    public String courseStandardSave(CourseStandard courseStandard){
        if (courseStandard == null) {
            return "false";
        }
        if (courseStandardService.saveCourseStandard(courseStandard)) {
            return "success";
        }
        return "false";
    }

    /**
     * 标准课程列表页
     * @return
     */
    @RequestMapping(value = "/to_course_standard_list")
    public ModelAndView toList() {
        ModelAndView modelAndView = new ModelAndView("course/course-standard-list");
        return modelAndView;
    }

    /**
     * 标准课程分页查询
     * @return
     */
    @RequestMapping(value = "/course_standard_list")
    public void courseStandardList(HttpServletResponse response,
                                   @RequestParam int pageIndex, @RequestParam int pageSize) {
        if (pageIndex > 0)
            --pageIndex;
        if (pageSize <=0)
            pageSize = 10;
        Map<String, Object> paramsMap = new HashMap();
        paramsMap.put("pageIndex", pageIndex);
        paramsMap.put("pageSize", pageSize);

        Map<String, Object> map = courseStandardService.queryCourseStandardList(paramsMap);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("courseStandardList", map.get("courseStandardList"));
        jsonObject.put("courseStandardCount", map.get("courseStandardCount"));
        responseSendMsg(response, jsonObject.toString());
    }

    /**
     * 标准课程删除
     * @param id 标准课程ID
     * @return
     */
    @RequestMapping(value = "course_standard_delete")
    @ResponseBody
    public String delete(@RequestParam Integer id) {
        int count = courseStandardService.delCourseStandardById(id);
        String result = "";
        if (count > 0) {
            result = "true";
        } else {
            result = "false";
        }
        return result;
    }


}
